package com.binvshe.binvshe.contacts;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.binvshe.binvshe.common.http.DataRequestListener;
import com.binvshe.binvshe.common.http.ListDataRequestListener;
import com.binvshe.binvshe.common.http.MyHttpRequest;
import com.binvshe.binvshe.common.http.UrlBase;
import com.binvshe.binvshe.common.http.UrlCenter;
import com.binvshe.binvshe.common.huanxin_im.ChatHelper;
import com.pachong.android.baseuicomponent.adapter.RecyclerViewBaseAdapter;
import com.pachong.android.baseuicomponent.fragment.SwipeListFragment;
import com.pachong.android.framework.httprequest.IHttpRequest;
import com.pachong.android.framework.httprequest.UrlParams;
import com.pachong.android.frameworkbase.dialog.AlertDialogFragment;
import com.pachong.android.frameworkbase.utils.EasyToast;

/**
 * Created by Zoi.
 * E-mail：KyluZoi@gmail.com
 * 2016/10/25
 */

public class FriendListFragment extends SwipeListFragment {
    private int mPage = 0;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        setPullToRefreshEnable(false);
        startLoading();
        return view;
    }

    @Override
    public RecyclerViewBaseAdapter createAdapter() {
        return new FriendListAdapter(getActivity());
    }


    private ListDataRequestListener<People> mResponceListener = new ListDataRequestListener<>(this, People.class);

    @Override
    public void onStartLoading() {
        if (mPage == 0 || mResponceListener.getResponseDataParser().hasMore()) {
            mPage++;
            MyHttpRequest request = new MyHttpRequest(getContext());
            UrlParams urlParams = new UrlParams();
            urlParams.put(UrlBase.ParamKey.PAGE, mPage + "");
            urlParams.put(UrlBase.ParamKey.SIZE, 20 + "");
            request.get(UrlCenter.FRIENDS, urlParams, mResponceListener);
        }
    }



    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long itemId) {
        People people = (People)getData().get(position);
        ChatHelper.startSingleChat(getActivity(), people.getPhone(), people.getNick_name(), people.getPortrait());
    }


    AlertDialogFragment deletePropFrag;
    @Override
    public boolean onItemLongClick(AdapterView<?> arg0, View arg1, final int position, long itemId) {
        deletePropFrag = new AlertDialogFragment();
        deletePropFrag.setMessage("确认删除好友?")
                .setNegativeButton("取消", true, new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        deletePropFrag.dismiss();
                    }
                })
                .setPositiveButton("确定", false, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        People friend = (People) getData().get(position);
                        deleteFriend(friend);
                    }
                }).show(getChildFragmentManager());

        return true;
    }

    private void deleteFriend(final People friend) {
        String url = UrlCenter.FRIENDS + "/" + friend.getUser_id();
        IHttpRequest request = new MyHttpRequest(getActivity());
        request.delete(url, null, new DataRequestListener<Object>(Object.class) {

            @Override
            public void onSuccess(Object acct) {
                super.onSuccess(acct);
                EasyToast.showToast(getActivity(), "好友删除成功");
                if (deletePropFrag != null) {
                    deletePropFrag.dismissAllowingStateLoss();
                    getData().remove(friend);
                    getAdapter().notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
               EasyToast.showToast(getActivity(), "好友删除失败");
                if (deletePropFrag != null) {
                    deletePropFrag.dismissAllowingStateLoss();

                }
            }
        });
    }

}
